/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.application;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author node1
 */
public class ReadWriteFile {
    public static String path = "./data/";
    public static String fileEXtension = ".data";
    
    public static synchronized long read(String fileName){
        FileReader reader = null;
        long result = -1;
        try {
            reader = new FileReader(path + fileName);
            BufferedReader br = new BufferedReader(reader);
            result = Long.parseLong(br.readLine());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ReadWriteFile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(ReadWriteFile.class.getName()).log(Level.SEVERE, null, ex);
            }
            return result;
        }
    }
    
    
    public static synchronized long write(String fileName, long value){
        long result = -1;
        FileWriter write = null;
        try {
            write = new FileWriter(path + fileName);
            BufferedWriter wr = new BufferedWriter(write);
            wr.write(value + "\n");
            wr.close();
            write.close();
            result = value;
        } catch (IOException ex) {
            Logger.getLogger(ReadWriteFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
